const Mock = require('mockjs')

const data = Mock.mock({
  'items|30': [{
    'id|+1': 0,
    workerNum: '@string("upper",2)' + '@string("number", 6)',
    username: '@string("lower",1)' + '@string("number",3,15)',
    password: '@string("lower",1)' + '@string("number",1)' + '@string(4, 18)',
    email: '@email', // 邮件 string
    phone: '13@string("number",9)', // 电话 string
    companyId: 0, // 公司 int
    departmentId: '@integer(0,4)', // 部门 int
    roleId: '@integer(0,4)', // 角色id int
    positionId: '@integer(0,9)', // 职位 int
    sex: '@integer(0,1)', // 性别(男：0，女：1) int
    state: '@integer(0,2)', // 状态id int（1：空闲，2：忙碌，3：已离职）
    createTime: '@date',
    birthday: '@date',
    resources: []
  }]
})

module.exports = [
  //分页查询用户
  {
    url: '/api/user/list',
    type: 'get',
    response: config => {
      const items = data.items

      const { example, pageSize, pageNum } = config.query
      const mockList = items.filter(user => {
        if (
          (!example.workerNum || user.workerNum == workerNum) &&
          (!example.username || user.username == username)) return true
        return false
      })
      const pageList = mockList.filter((item, index) => index < pageSize * pageNum && index >= pageSize * (pageNum - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  //根据id删除用户
  {
    url: '/api/user/deleteById',
    type: 'get',
    response: config => {
      const { id } = config.query
      data.items = data.items.filter(user => user.id != id)
      return {
        code: 20000,
        message: "删除成功"
      }
    }
  },
  //新增用户
  {
    url: '/api/user/addUser',
    type: 'post',
    response: config => {
      const { user } = config.body
      user.id = data.items[data.items.length - 1].id + 1
      user.state = 0
      data.items.push(user)
      return {
        code: 20000,
        message: "添加成功"
      }
    }
  },
  //根据id查找用户
  {
    url: '/api/user/queryByWorkerNum',
    type: 'get',
    response: config => {
      const { workerNum } = config.query
      let find = null
      data.items.forEach(item => {
        if (item.workerNum == workerNum) {
          find = { ...item }
          return
        }
      })
      return {
        code: 20000,
        data: {
          user: find
        }
      }
    }
  },
  //根据用户名查找用户
  {
    url: '/api/user/queryByUsername',
    type: 'get',
    response: config => {
      const { username } = config.query
      let find = null
      data.items.forEach(item => {
        if (item.username == username) {
          find = { ...item }
          return
        }
      })
      return {
        code: 20000,
        data: {
          user: find
        }
      }
    }
  },
  //更新用户
  {
    url: '/api/user/updateUser',
    type: 'post',
    response: config => {
      const { user } = config.body
      data.items.forEach((item, index) => {
        if (item.id == user.id) {
          data.items.splice(index, 1, user)
        }
      })
      return {
        code: 20000,
        message: "修改成功"
      }
    }
  },
]
